package com.uzai.common.dto.base;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.MDC;

import java.io.Serializable;
import java.util.Objects;

/**
 * @Auther: jianghan
 * @Date: 2021/9/8 14:37
 * @Description:
 */
public class UzaiRespVo<T> implements Serializable {
    private static final long serialVersionUID = 6601659703478237629L;
    private int code;
    private String msg;
    private long time = System.currentTimeMillis();
    private T data;

    private String traceId = StringUtils.isBlank(MDC.get("X-B3-TraceId")) ? MDC.get("traceId") : MDC.get("X-B3-TraceId");

    public static <T> UzaiRespVo<T> fail(String message) {

        UzaiRespVo<T> res = new UzaiRespVo<>();
        res.setCode(500);
        res.setMsg(message);
        return res;
    }

    public static <T> UzaiRespVo<T> fail(Integer code, String msg) {
        UzaiRespVo<T> restWrapperVo = new UzaiRespVo<>();
        restWrapperVo.setCode(code);
        restWrapperVo.setMsg(msg);
        return restWrapperVo;
    }

    public static <T> UzaiRespVo<T> noAuth(String message, T data) {

        UzaiRespVo<T> res = new UzaiRespVo<>();
        res.setCode(401);
        res.setData(data);
        res.setMsg(message);
        res.setTime(System.currentTimeMillis());
        return res;
    }

    public static <T> UzaiRespVo<T> success(T data) {

        UzaiRespVo<T> res = new UzaiRespVo<>();
        res.setCode(200);
        res.setData(data);
        res.setMsg("success");
        res.setTime(System.currentTimeMillis());
        return res;
    }


    public int getCode() {
        return code;
    }

    public void setCode(int code) {
        this.code = code;
    }

    public String getMsg() {
        return msg;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }

    public long getTime() {
        return time;
    }

    public void setTime(long time) {
        this.time = time;
    }

    public T getData() {
        return data;
    }

    public void setData(T data) {
        this.data = data;
    }

    public String getTraceId() {
        return traceId;
    }

    public void setTraceId(String traceId) {
        this.traceId = traceId;
    }

    public boolean isSuccess() {
        return Objects.equals(code, 200) && Objects.nonNull(data);
    }
}
